package com.nbjtjc.safe.service.edu;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduEntryTrainingCompanyMapper;
import com.nbjtjc.safe.mapper.edu.EduEntryTrainingDepartmentMapper;
import com.nbjtjc.safe.mapper.edu.EduEntryTrainingMapper;
import com.nbjtjc.safe.mapper.edu.EduEntryTrainingTeamMapper;
import com.nbjtjc.safe.mapper.edu.EduRequirementSurveyItemMapper;
import com.nbjtjc.safe.mapper.sys.SysDeptMapper;
import com.nbjtjc.safe.mapper.sys.SysUserMapper;
import com.nbjtjc.safe.model.edu.EduEntryTraining;
import com.nbjtjc.safe.model.edu.EduEntryTrainingCompany;
import com.nbjtjc.safe.model.edu.EduEntryTrainingDepartment;
import com.nbjtjc.safe.model.edu.EduEntryTrainingTeam;
import com.nbjtjc.safe.model.edu.EduRequirementInvestigation;
import com.nbjtjc.safe.model.sys.SysUser;
import com.nbjtjc.safe.util.MyShiroUtils;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.collection.CollectionUtil;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class EduEntryTrainingService {
	@Autowired
	EduEntryTrainingMapper eduEntryTrainingMapper;
	@Autowired
	EduEntryTrainingCompanyMapper eduEntryTrainingCompanyMapper;
	@Autowired
	EduEntryTrainingDepartmentMapper eduEntryTrainingDepartmentMapper;
	@Autowired
	EduEntryTrainingTeamMapper eduEntryTrainingTeamMapper;
	@Autowired
	SysUserMapper sysUserMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    
    @Transactional(rollbackFor = {Throwable.class})
	  public Result add(EduEntryTraining eduEntryTraining){
	        Result result=new Result();
	        if(ObjectUtil.isNull(eduEntryTraining)
	        		||CollectionUtil.isEmpty(eduEntryTraining.getEduUserIds())
	        		){
	            result.setFalse("培训员工ID不能为空");
	            return result;
	        }

	        List<Integer> userIds = new ArrayList();
	        //获取员工编号数组
	        userIds = eduEntryTraining.getEduUserIds();
	        
	        SysUser sysUserTemp = new SysUser();
	        
        	int id = -1;
	        for(int userId : userIds){
	        	eduEntryTraining.setUserId(userId);
	        	eduEntryTraining.setId(null);
	        	int  companeyId= MyShiroUtils.getCompanyId();
	        	eduEntryTraining.setCompanyId(companeyId);
	        	eduEntryTraining.setCompanyName(sysDeptMapper.findNameById(companeyId));
	        	eduEntryTraining.setCreateDate(new Date());
	        	eduEntryTraining.setCreateId(MyShiroUtils.getSessionUserId());
	        	
	        	
	        	int res=eduEntryTrainingMapper.insertSelective(eduEntryTraining);
		        if(res==0){
		            result.setFalse("插入失败");
		            return result;
		        }
	        	id = eduEntryTraining.getId();

	        	//人员表中,人员中已三级培训字段变为true
	        	sysUserTemp = sysUserMapper.selectByPrimaryKey(userId);
	        	sysUserTemp.setEntryTrain(true);
	        	res = sysUserMapper.updateByPrimaryKeySelective(sysUserTemp);
	        	
		        //插入公司培训数据
		        	eduEntryTraining.getEduEntryTrainingCompany().setTrainingId(id);
		        	eduEntryTraining.getEduEntryTrainingCompany().setId(null);
			    	res=eduEntryTrainingCompanyMapper.insertSelective(eduEntryTraining.getEduEntryTrainingCompany());
			    	if(res==0){
			            result.setFalse("插入公司培训失败");
			            return result;
			        }
		        
		        //插入部门培训数据
		        	eduEntryTraining.getEduEntryTrainingDepartment().setTrainingId(id);
		        	eduEntryTraining.getEduEntryTrainingDepartment().setId(null);
			    	res=eduEntryTrainingDepartmentMapper.insertSelective(eduEntryTraining.getEduEntryTrainingDepartment());
			    	if(res==0){
			            result.setFalse("插入部门培训失败");
			            return result;
			        }
		           
		        //插入班组培训数据
		        	eduEntryTraining.getEduEntryTrainingTeam().setTrainingId(id);
		        	eduEntryTraining.getEduEntryTrainingTeam().setId(null);
			    	res=eduEntryTrainingTeamMapper.insertSelective(eduEntryTraining.getEduEntryTrainingTeam());
			    	if(res==0){
			            result.setFalse("插入班组培训失败");
			            return result;
			        }
		        }
	        
	      
	        result.setData(eduEntryTraining);
	        return result;
	    }

	  public Result addAndUpdateOne(EduEntryTraining eduEntryTraining){
	        Result result=new Result();
	        if(ObjectUtil.isNull(eduEntryTraining)
	        		||ObjectUtil.isNull(eduEntryTraining.getUserId())
	        		){
	            result.setFalse("培训员工ID不能为空");
	            return result;
	        }

	        if(ObjectUtil.isNull(eduEntryTraining.getId())
	        		|| ObjectUtil.isNull(eduEntryTrainingMapper.selectByPrimaryKey(eduEntryTraining.getId()))){
	        	
	        	int  companeyId= MyShiroUtils.getCompanyId();
	        	eduEntryTraining.setCompanyId(companeyId);
	        	eduEntryTraining.setCompanyName(sysDeptMapper.findNameById(companeyId));
	        	eduEntryTraining.setCreateDate(new Date());
	        	eduEntryTraining.setCreateId(MyShiroUtils.getSessionUserId());
	        	
	        	int res=eduEntryTrainingMapper.insertSelective(eduEntryTraining);
		            
		        if(res==0){
		            result.setFalse("插入失败");
		            return result;
		        }
		        
	        	//人员表中,人员中已三级培训字段变为true
		        SysUser sysUserTemp = new SysUser();
	        	sysUserTemp = sysUserMapper.selectByPrimaryKey(eduEntryTraining.getUserId());
	        	sysUserTemp.setEntryTrain(true);
	        	res = sysUserMapper.updateByPrimaryKeySelective(sysUserTemp);
		        if(res==0){
		            result.setFalse("更新人员已三级培训字段失败");
		            return result;
		        }
		        
		        //插入公司培训数据
		        if(ObjectUtil.isNotNull(eduEntryTraining.getEduEntryTrainingCompany())){
		        	eduEntryTraining.getEduEntryTrainingCompany().setTrainingId(eduEntryTraining.getId());
			    	res=eduEntryTrainingCompanyMapper.insertSelective(eduEntryTraining.getEduEntryTrainingCompany());
			    	if(res==0){
			            result.setFalse("插入公司培训失败");
			            return result;
			        }
		        }
		        //插入部门培训数据
		        if(ObjectUtil.isNotNull(eduEntryTraining.getEduEntryTrainingDepartment())){
		        	eduEntryTraining.getEduEntryTrainingDepartment().setTrainingId(eduEntryTraining.getId());
			    	res=eduEntryTrainingDepartmentMapper.insertSelective(eduEntryTraining.getEduEntryTrainingDepartment());
			    	if(res==0){
			            result.setFalse("插入部门培训失败");
			            return result;
			        }
		        }     
		        //插入班组培训数据
		        if(ObjectUtil.isNotNull(eduEntryTraining.getEduEntryTrainingTeam())){
		        	eduEntryTraining.getEduEntryTrainingTeam().setTrainingId(eduEntryTraining.getId());
			    	res=eduEntryTrainingTeamMapper.insertSelective(eduEntryTraining.getEduEntryTrainingTeam());
			    	if(res==0){
			            result.setFalse("插入班组培训失败");
			            return result;
			        }
		        }
	        }else
	        {
	        	eduEntryTraining.setModifyDate(new Date());
	        	eduEntryTraining.setModifyId(MyShiroUtils.getSessionUserId());
	        	int res=eduEntryTrainingMapper.updateByPrimaryKeySelective(eduEntryTraining);
	        	 if(res==0){
	        		 result.setFalse("更新失败");
			         return result;

	        	 }
		        //更新公司培训数据
	        	 EduEntryTrainingCompany inputEduCompany = eduEntryTraining.getEduEntryTrainingCompany();
		    	 //inputEduCompany不为空则新建或更新公司培训数据
	        	 if(ObjectUtil.isNotNull(inputEduCompany)){
		        	 //inputEduCompany的trainingId(如果存在)与eduEntryTraining的Id需一致，否则报错
	        		 if(ObjectUtil.isNotNull(inputEduCompany.getTrainingId())&&!(inputEduCompany.getTrainingId().equals(eduEntryTraining.getId()))){
				            result.setFalse("录入公司培训数据与入职培训id不符，更新失败");
				            return result;
	        		 }	 
	        		 inputEduCompany.setTrainingId(eduEntryTraining.getId());
	        		 //按trainingId查到的库中的公司培训数据
	        		 EduEntryTrainingCompany dataBaseEduCompany = eduEntryTrainingCompanyMapper.findByTrainingId(eduEntryTraining.getId());
	        		 //库中公司培训数据为空则插入，不为空且录入/库中两个ID一致则更新，不一致报错
	        		 if(ObjectUtil.isNotNull(dataBaseEduCompany)){
	        			//录入的eduEntryTrainingCompany中是否有id
			    		 if(ObjectUtil.isNotNull(inputEduCompany.getId())){
			    			 //如果有id,必须与库中的id一致,否则报错
			    			 if(!(inputEduCompany.getId().equals(dataBaseEduCompany.getId())) ){
						            result.setFalse("录入公司培训ID与库中不一致，更新失败");
						            return result;
			    			 }
			    		 }else{
			    			 //如果没有id，则将库中按trainingid查到的EduEntryTrainingCompany的ID赋给录入的eduEntryTraining中的eduEntryTrainingCompany
			    			 inputEduCompany.setId(dataBaseEduCompany.getId());
			    		 }
	        	 		res=eduEntryTrainingCompanyMapper.updateByPrimaryKeySelective(eduEntryTraining.getEduEntryTrainingCompany());
				    	if(res==0){
				            result.setFalse("更新公司培训失败");
				            return result;
				        }
	        		 }else{
	 			    	res=eduEntryTrainingCompanyMapper.insertSelective(inputEduCompany);
				    	if(res==0){
				            result.setFalse("插入公司培训失败");
				            return result;
				    	}
	        		 }
			     }
		        //更新部门培训数据
	        	 EduEntryTrainingDepartment inputEduDepartment = eduEntryTraining.getEduEntryTrainingDepartment();
		    	 //inputEduDepartment不为空则新建或更新部门培训数据
	        	 if(ObjectUtil.isNotNull(inputEduDepartment)){
		        	 //inputEduDepartment的trainingId与eduEntryTraining的Id需一致，否则报错
	        		 if(ObjectUtil.isNotNull(inputEduDepartment.getTrainingId())&&!(inputEduDepartment.getTrainingId().equals(eduEntryTraining.getId()))){
				            result.setFalse("录入部门培训数据与入职培训id不符，更新失败");
				            return result;
	        		 }	 
	        		 inputEduDepartment.setTrainingId(eduEntryTraining.getId());
	        		 //按trainingId查到的库中的部门培训数据
	        		 EduEntryTrainingDepartment dataBaseEduDepartment = eduEntryTrainingDepartmentMapper.findByTrainingId(eduEntryTraining.getId());
	        		 //库中部门培训数据为空则插入，不为空且录入/库中两个ID一致则更新，不一致报错
	        		 if(ObjectUtil.isNotNull(dataBaseEduDepartment)){
	        			//录入的eduEntryTrainingDepartment中是否有id
			    		 if(ObjectUtil.isNotNull(inputEduDepartment.getId())){
			    			 //如果有id,必须与库中的id一致,否则报错
			    			 if(!(inputEduDepartment.getId().equals(dataBaseEduDepartment.getId()))){
						            result.setFalse("录入部门培训ID与库中不一致，更新失败");
						            return result;
			    			 }
			    		 }else{
			    			 //如果没有id，则将库中按trainingid查到的EduEntryTrainingDepartment的ID赋给录入的eduEntryTraining中的eduEntryTrainingDepartment
			    			 inputEduDepartment.setId(dataBaseEduDepartment.getId());
			    		 }
	        	 		res=eduEntryTrainingDepartmentMapper.updateByPrimaryKeySelective(eduEntryTraining.getEduEntryTrainingDepartment());
				    	if(res==0){
				            result.setFalse("更新部门培训失败");
				            return result;
				        }
	        		 }else{
	 			    	res=eduEntryTrainingDepartmentMapper.insertSelective(inputEduDepartment);
				    	if(res==0){
				            result.setFalse("插入部门培训失败");
				            return result;
				    	}
	        		 }
			     }
	        	 
//	        	 
//			     if(ObjectUtil.isNotNull(eduEntryTraining.getEduEntryTrainingDepartment().getId())){
//			        if(ObjectUtil.isNotNull(eduEntryTraining.getEduEntryTrainingDepartment())){
//				    	res=eduEntryTrainingDepartmentMapper.updateByPrimaryKeySelective(eduEntryTraining.getEduEntryTrainingDepartment());
//				    	if(res==0){
//				            result.setFalse("更新部门培训失败");
//				            return result;
//				        }
//			        } 
//			     }else{
//				    	res=eduEntryTrainingDepartmentMapper.insertSelective(eduEntryTraining.getEduEntryTrainingDepartment());
//				    	if(res==0){
//				            result.setFalse("插入部门培训失败");
//				            return result;
//				        }
//			     }
			     
		        //更新班组培训数据
	        	 EduEntryTrainingTeam inputEduTeam = eduEntryTraining.getEduEntryTrainingTeam();
		    	 //inputEduTeam不为空则新建或更新班组培训数据
	        	 if(ObjectUtil.isNotNull(inputEduTeam)){
		        	 //inputEduTeam的trainingId与eduEntryTraining的Id需一致，否则报错
	        		 if(ObjectUtil.isNotNull(inputEduTeam.getTrainingId())&&!(inputEduTeam.getTrainingId().equals(eduEntryTraining.getId()))){
				            result.setFalse("录入班组培训数据与入职培训id不符，更新失败");
				            return result;
	        		 }	 
	        		 inputEduTeam.setTrainingId(eduEntryTraining.getId());
	        		 //按trainingId查到的库中的班组培训数据
	        		 EduEntryTrainingTeam dataBaseEduTeam = eduEntryTrainingTeamMapper.findByTrainingId(eduEntryTraining.getId());
	        		 //库中班组培训数据为空则插入，不为空且录入/库中两个ID一致则更新，不一致报错
	        		 if(ObjectUtil.isNotNull(dataBaseEduTeam)){
	        			//录入的eduEntryTrainingTeam中是否有id
			    		 if(ObjectUtil.isNotNull(inputEduTeam.getId())){
			    			 //如果有id,必须与库中的id一致,否则报错
			    			 if(!(inputEduTeam.getId().equals(dataBaseEduTeam.getId()) )){
						            result.setFalse("录入班组培训ID与库中不一致，更新失败");
						            return result;
			    			 }
			    		 }else{
			    			 //如果没有id，则将库中按trainingid查到的EduEntryTrainingTeam的ID赋给录入的eduEntryTraining中的eduEntryTrainingTeam
			    			 inputEduTeam.setId(dataBaseEduTeam.getId());
			    		 }
	        	 		res=eduEntryTrainingTeamMapper.updateByPrimaryKeySelective(eduEntryTraining.getEduEntryTrainingTeam());
				    	if(res==0){
				            result.setFalse("更新班组培训失败");
				            return result;
				        }
	        		 }else{
	 			    	res=eduEntryTrainingTeamMapper.insertSelective(inputEduTeam);
				    	if(res==0){
				            result.setFalse("插入班组培训失败");
				            return result;
				    	}
	        		 }
			     }
	        	 
//			     if(ObjectUtil.isNotNull(eduEntryTraining.getEduEntryTrainingDepartment().getId())){
//			        if(ObjectUtil.isNotNull(eduEntryTraining.getEduEntryTrainingTeam())){
//				    	res=eduEntryTrainingTeamMapper.updateByPrimaryKeySelective(eduEntryTraining.getEduEntryTrainingTeam());
//				    	if(res==0){
//				            result.setFalse("更新班组培训失败");
//				            return result;
//				        }
//			        }
//			     }else{
//				    	res=eduEntryTrainingTeamMapper.insertSelective(eduEntryTraining.getEduEntryTrainingTeam());
//				    	if(res==0){
//				            result.setFalse("插入班组培训失败");
//				            return result;
//				        }
//			     }
	        }
	        result.setData(eduEntryTraining);
	        return result;
	    }

	    @Transactional(rollbackFor = {Throwable.class})
	public Result delete(EduEntryTraining eduEntryTraining){
	    Result result=new Result();
	    if(ObjectUtil.isNull(eduEntryTraining.getId())||ObjectUtil.isNull(eduEntryTraining.getUserId())){
	        result.setFalse("删除对象不能为空,用户ID不能为空");
	        return result;
	    }
	    
    	//人员表中,人员中已三级培训字段变为false
	    SysUser sysUserTemp = new SysUser();
	    
    	sysUserTemp = sysUserMapper.selectByPrimaryKey(eduEntryTraining.getUserId());
    	sysUserTemp.setEntryTrain(false);
    	eduEntryTraining.setModifyDate(new Date());
    	eduEntryTraining.setModifyId(MyShiroUtils.getSessionUserId());
    	int res = sysUserMapper.updateByPrimaryKeySelective(sysUserTemp);
    	
	     res=eduEntryTrainingMapper.deleteByPrimaryKey(eduEntryTraining.getId());
	    if(res==0){
	        result.setFalse("删除失败");
	        return result;
	        }
	    return result;
	}
	
	//根据条件查找调查项目
	
	public Result find(EduEntryTraining eduEntryTraining) {
	    Result result = new Result();
	    PageHelper.startPage(eduEntryTraining.getPageCurrent(), eduEntryTraining.getPageSize());
	    eduEntryTraining.setCompanyId(MyShiroUtils.getCompanyId());

	    List<EduEntryTraining> eduEntryTrainings = eduEntryTrainingMapper.search(eduEntryTraining);
	    result.setData(new PageInfo<>(eduEntryTrainings));
	
	    return result;
	}
	
	public Result findDetail(EduEntryTraining eduEntryTraining) {
	    Result result = new Result();
	    PageHelper.startPage(eduEntryTraining.getPageCurrent(), eduEntryTraining.getPageSize());
//	    eduEntryTraining.setCompanyId(MyShiroUtils.getCompanyId());
	    
	    
	    List<EduEntryTraining> eduEntryTrainings = eduEntryTrainingMapper.searchDetail(eduEntryTraining);
	    result.setData(new PageInfo<>(eduEntryTrainings));
	
	    return result;
	}
	

	public Result batchInputScores(EduEntryTraining eduEntryTraining) {
		// TODO Auto-generated method stub
	    Result result = new Result();
        List<Integer> ids = new ArrayList();
        List<Integer> scores = new ArrayList();
        ids = eduEntryTraining.getIds();
        scores=eduEntryTraining.getScores();
        int res = 0;
        for(int i = 0;i<ids.size();i++){
        	EduEntryTraining entryTraining = eduEntryTrainingMapper.selectByPrimaryKey(ids.get(i));
        	entryTraining.setScore(scores.get(i));
        	entryTraining.setStatus(1);
        	
        	entryTraining.setModifyDate(new Date());
        	entryTraining.setModifyId(MyShiroUtils.getSessionUserId());
        	
        	res = 0;
        	res = eduEntryTrainingMapper.updateByPrimaryKeySelective(entryTraining);
    	    if(res==0){
    	        result.setFalse("更新分数失败");
    	        return result;
    	        }
        }
	    result.setData(eduEntryTraining);

		return result;
	}
}
